package webapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AdminDelete
 */
public class AdminDelete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminDelete() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("We are in Service method of AdminDelete");
		PrintWriter out=response.getWriter();
		HttpSession session=request.getSession(false); 
		String un = (String) session.getAttribute("name");
		
        if(un!=null){
		
		 String em=request.getParameter("email");
		 
		 String msg=" ";
	
		 
		 Connection conn = null;
         String url = "jdbc:mysql://localhost:3306/";
         String dbName = "webappschema";
         String driver = "com.mysql.jdbc.Driver";
         String dbusername = "root";
         String dbpassword = "webapp";
         
         try {
             Class.forName(driver).newInstance();
             conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
             String strQuery = "Delete from login where Email= '" + em + "' " ;
             Statement st = conn.createStatement();
             int rs = st.executeUpdate(strQuery);
             
             if(rs>0){ 
            	 
            	   out.println("<script type=\"text/javascript\">");
	    	 	   out.println("alert('Record successfully deleted');");
	    	 	   out.println("location='AdminDelete.jsp';");
	    	 	   out.println("</script>");
            	 
        	   }
         
             else
             {
              msg = "This email is not registered";
              
              response.setContentType("text/html");
    			
    			 out.println("<font size = '6' color=red>" + msg + "</font>");
             }
             
             
             //rs.close();
             st.close();
             } catch (Exception e) {
               e.printStackTrace();
             }
        }
         else{  
				out.print("Please login first");    
	            request.getRequestDispatcher("index.jsp").include(request, response);  
	        } 
	      //  out.close();
  
    
    }
}
